package com.github.pig.nideshop.weixin.handler;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.nideshop.entity.NideshopUser;
import com.github.pig.nideshop.service.INideshopUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
@Slf4j
public class UnsubscribeHandler extends AbstractHandler {
  @Autowired
  private INideshopUserService nideshopUserService;
  @Override
  public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                  Map<String, Object> context, WxMpService wxMpService,
                                  WxSessionManager sessionManager) {
    String openId = wxMessage.getFromUser();
    this.logger.info("取消关注用户 OPENID: " + openId);
    NideshopUser user=new NideshopUser();
    user.setSubscribe(0);
    EntityWrapper<NideshopUser> ex=new EntityWrapper();
    ex.eq("weixin_openid",openId);
    log.info("用户取消关注处理:{}",nideshopUserService.update(user,ex));
    return null;
  }

}
